package com.nmc.webapp.action;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.nmc.common.BirthStatusEnum;
import com.nmc.common.Constants;
import com.nmc.service.manager.BirthManager;
import com.nmc.service.manager.HospitalManager;
import com.nmc.service.manager.ZonesManager;
import com.nmc.service.model.Birth;
import com.nmc.service.model.Hospital;
import com.nmc.service.model.LabelValue;
import com.nmc.service.model.StatisticsModel;

/**
 * Action to allow new users to sign up.
 */
public class DashboardAction extends BaseAction {
    private static final long serialVersionUID = 6558317334878272308L;
    private Hospital hospital;
    private String cancel;
    private HospitalManager hospitalManager;
    private ZonesManager zonesManager;
    private BirthManager birthManager;
    private List<Hospital> hospitalList = new ArrayList<Hospital>();
    private List<Birth> birthList = new ArrayList<Birth>();
    private List<Birth> rejectedList = new ArrayList<Birth>();
    private List<Birth> approvedList = new ArrayList<Birth>();
    private int hospitalId;
    private int zoneId;
    
    
    private List<LabelValue> adminStatus = new ArrayList<LabelValue>();
    
    private List<LabelValue> hospitalStatus = new ArrayList<LabelValue>();
    
    private List<LabelValue> zoneStatus = new ArrayList<LabelValue>();
    
    
    public List<LabelValue> getZoneStatus() {
		return zoneStatus;
	}

	public void setZoneStatus(List<LabelValue> zoneStatus) {
		this.zoneStatus = zoneStatus;
	}

	public List<LabelValue> getHospitalStatus() {
		return hospitalStatus;
	}

	public void setHospitalStatus(List<LabelValue> hospitalStatus) {
		this.hospitalStatus = hospitalStatus;
	}

	public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    /**
     * Returns "input"
     * @return "input" by default
     */
    public String doDefault() {
    	
        return INPUT;
    }
    
    public String dashBoard(){
    	if (getRequest().isUserInRole(Constants.ADMIN_ROLE)) {
    		
    		setAdminStatus();
    		return "admin";
    	}else if (getRequest().isUserInRole(Constants.HOSPITAL_ROLE)){
    		hospitalId = userManager.getUserByUsername(getRequest().getRemoteUser()).getHospitalId();
    		getSubmissionPendingBirthsRecords();
    		getRejectedBirthsRecords();
    		setHospitalStats();
    		birthManager.getHospitalMaleFemaleCountByMonths(hospitalId,6);
    		return "hospital";
    	} else if (getRequest().isUserInRole(Constants.ZONE_ROLE_BIRTH)){
    		zoneId = userManager.getUserByUsername(getRequest().getRemoteUser()).getZoneId();
    		getApprovalPendingBirthsRecords();
    		getApprovedBirthsRecords();
    		setZoneStats();
    		return "zone";
    	} else if (getRequest().isUserInRole(Constants.ZONE_ROLE_DEATH)){
    		zoneId = userManager.getUserByUsername(getRequest().getRemoteUser()).getZoneId();
    		getApprovalPendingBirthsRecords();
    		getApprovedBirthsRecords();
    		setZoneStats();
    		return "zonedeath";
    	} else {
    		return "dataAccessFailure";
    	}
    }
    
    private void setAdminStatus() {
    	StatisticsModel statisticsModel = 	birthManager.getAllZoneStatisticsCount(6);
		LabelValue labelValue = new LabelValue("Total Births", ""+statisticsModel.getTotalBirthCount());
		LabelValue labelValue1 = new LabelValue("Male Birth", ""+statisticsModel.getMaleCount());
		LabelValue labelValue2 = new LabelValue("Female Birth", ""+statisticsModel.getFemaleCount());
		LabelValue labelValue3 = new LabelValue("Twins", ""+statisticsModel.getTwinsCount());
		LabelValue labelValue4 = new LabelValue("Triplets", ""+statisticsModel.getTripletsCount());
		LabelValue labelValue5 = new LabelValue("Still Birth", ""+statisticsModel.getStillBirthCount());
		LabelValue labelValue6 = new LabelValue("Registered Hospitals", ""+statisticsModel.getZoneHospitalsCount());
		
		
		adminStatus.add(labelValue);
		adminStatus.add(labelValue1);
		adminStatus.add(labelValue2);
		adminStatus.add(labelValue3);
		adminStatus.add(labelValue4);
		adminStatus.add(labelValue5);
		adminStatus.add(labelValue6);
		
	}

	public List<LabelValue> getAdminStatus() {
		return adminStatus;
	}

	public void setAdminStatus(List<LabelValue> adminStatus) {
		this.adminStatus = adminStatus;
	}

	private void setHospitalStats() {
    	StatisticsModel statisticsModel = 	birthManager.getHospitalStatisticsCount(hospitalId,6);
		LabelValue labelValue = new LabelValue("Total Births", ""+statisticsModel.getTotalBirthCount());
		LabelValue labelValue1 = new LabelValue("Male Birth", ""+statisticsModel.getMaleCount());
		LabelValue labelValue2 = new LabelValue("Female Birth", ""+statisticsModel.getFemaleCount());
		LabelValue labelValue3 = new LabelValue("Twins", ""+statisticsModel.getTwinsCount());
		LabelValue labelValue4 = new LabelValue("Triplets", ""+statisticsModel.getTripletsCount());
		LabelValue labelValue5 = new LabelValue("Still Birth", ""+statisticsModel.getStillBirthCount());
		
		hospitalStatus.add(labelValue);
		hospitalStatus.add(labelValue1);
		hospitalStatus.add(labelValue2);
		hospitalStatus.add(labelValue3);
		hospitalStatus.add(labelValue4);
		hospitalStatus.add(labelValue5);
		
		
	}

    private void setZoneStats() {
    	StatisticsModel statisticsModel = 	birthManager.getZoneStatisticsCount(zoneId,6);
		LabelValue labelValue = new LabelValue("Total Births", ""+statisticsModel.getTotalBirthCount());
		LabelValue labelValue1 = new LabelValue("Male Birth", ""+statisticsModel.getMaleCount());
		LabelValue labelValue2 = new LabelValue("Female Birth", ""+statisticsModel.getFemaleCount());
		LabelValue labelValue3 = new LabelValue("Twins", ""+statisticsModel.getTwinsCount());
		LabelValue labelValue4 = new LabelValue("Triplets", ""+statisticsModel.getTripletsCount());
		LabelValue labelValue5 = new LabelValue("Still Birth", ""+statisticsModel.getStillBirthCount());
		LabelValue labelValue6 = new LabelValue("Registered Hospitals", ""+statisticsModel.getZoneHospitalsCount());
		
		zoneStatus.add(labelValue);
		zoneStatus.add(labelValue1);
		zoneStatus.add(labelValue2);
		zoneStatus.add(labelValue3);
		zoneStatus.add(labelValue4);
		zoneStatus.add(labelValue5);
		zoneStatus.add(labelValue6);
		
	}
    
	/**
     * This method will initialize the birthList with all records in Draft mode for that hospital
     */
    private void getSubmissionPendingBirthsRecords() {
    	HttpServletRequest request = getRequest();
    	//int hospitalId = userManager.getUserByUsername(request.getRemoteUser()).getHospitalId();
    	birthList = birthManager.getHospitalBirthListByStatus(hospitalId, BirthStatusEnum.Draft, 6);
    }
    
    /**
     * This method will initialize the birthList with all records awaiting approval from that zone
     */
    private void getApprovalPendingBirthsRecords() {
    	HttpServletRequest request = getRequest();
    	//int zoneId = userManager.getUserByUsername(request.getRemoteUser()).getZoneId();
    	birthList = birthManager.getZoneBirthListByStatus(zoneId, BirthStatusEnum.Submit, 6); 
    }
    
    /**
     * This method will initialize the rejectedList with all records recently rejected from that zone
     */
    private void getRejectedBirthsRecords() {
    	HttpServletRequest request = getRequest();
    	//int zoneId = userManager.getUserByUsername(request.getRemoteUser()).getZoneId();
    	rejectedList = birthManager.getHospitalBirthListByStatus(hospitalId, BirthStatusEnum.Rejected,6); 
    }
    
    /**
     * This method will initialize the approvedList with all records recently approved from that zone
     */
    private void getApprovedBirthsRecords() {
    	HttpServletRequest request = getRequest();
    	//int zoneId = userManager.getUserByUsername(request.getRemoteUser()).getZoneId();
    	approvedList = birthManager.getZoneBirthListByStatus(zoneId, BirthStatusEnum.Approved, 6); 
    }
    public String searchHospital(){
    	
    	return SUCCESS;
    }

    public String searchBirth(){
    	
    	return SUCCESS;
    }
   
    public String hospitalDashboard(){
    	getSubmissionPendingBirthsRecords();
		getRejectedBirthsRecords();
		setHospitalStats();
    	return "hospital";
    }

    public String zoneDashboard(){
    	getApprovalPendingBirthsRecords();
		getApprovedBirthsRecords();
		setZoneStats();
		return "zone";
    }
	public void setHospital(Hospital hospital) {
		this.hospital = hospital;
	}


	public Hospital getHospital() {
		return hospital;
	}


	public void setHospitalManager(HospitalManager hospitalManager) {
		this.hospitalManager = hospitalManager;
	}


	public HospitalManager getHospitalManager() {
		return hospitalManager;
	}


	public void setZonesManager(ZonesManager zonesManager) {
		this.zonesManager = zonesManager;
	}


	public ZonesManager getZonesManager() {
		return zonesManager;
	}

	/**
	 * @return the hospitalList
	 */
	public List<Hospital> getHospitalList() {
		return hospitalList;
	}

	/**
	 * @param hospitalList the hospitalList to set
	 */
	public void setHospitalList(List<Hospital> hospitalList) {
		this.hospitalList = hospitalList;
	}

	/**
	 * @return the birthList
	 */
	public List<Birth> getBirthList() {
		return birthList;
	}

	/**
	 * @param birthList the birthList to set
	 */
	public void setBirthList(List<Birth> birthList) {
		this.birthList = birthList;
	}

	public BirthManager getBirthManager() {
		return birthManager;
	}

	public void setBirthManager(BirthManager birthManager) {
		this.birthManager = birthManager;
	}

	public List<Birth> getRejectedList() {
		return rejectedList;
	}

	public void setRejectedList(List<Birth> rejectedList) {
		this.rejectedList = rejectedList;
	}

	public List<Birth> getApprovedList() {
		return approvedList;
	}

	public void setApprovedList(List<Birth> approvedList) {
		this.approvedList = approvedList;
	}

	public void setHospitalId(int hospitalId) {
		this.hospitalId = hospitalId;
	}

	public int getHospitalId() {
		return hospitalId;
	}

	public void setZoneId(int zoneId) {
		this.zoneId = zoneId;
	}

	public int getZoneId() {
		return zoneId;
	}


	
}
